package appli;


import java.util.ArrayList;
import java.util.List;

import appli.utils.Config;

import ihm.AppliWindow;
import sma.environment.LocalEnvironment;

/**
 * This class launches the application.
 * 
 * Since 0.3 version, it can handle several telemeter in the same application
 * 
 * @author Etienne Sainton
 * @since 0.1
 * @version 0.3
 */
public class Launcher {
	
	/**
	 * Telemeter list used by application
	 * @since 0.3
	 */
	public static List<Telemeter> listTelemeters = new ArrayList<Telemeter>();
	
	/**
	 * Thread dedicated to send the direction to the vehicle
	 */
	public static Direction direction;
	
	private SmaLauncher sma;
	
	private AppliWindow appliWindow;
	/**
	 * Launch all application
	 * @since 0.1
	 */
	public Launcher() {
		//Load config
		Config.loadConfig();

		//Create GUI
		appliWindow = new AppliWindow(this);
		
	}

	/**
	 * Application launcher
	 * @param args useless for now, we don't need any args
	 */
	public static void main(String[] args) {
		
		new Launcher();
	}
	
	/**
	 * Lance toute la simulation : d'abord les télémètres, puis le SMA, puir la GUI
	 */
	public void startSimu(){
		//Create environment
		LocalEnvironment env = new LocalEnvironment();
		
		//Create SMA
		sma = new SmaLauncher(env);
		
		appliWindow.setSma(sma);
		
		//Create telemeters
		createTelemeters();
		
		//direction = new Direction(Config.DIRECTION_ADDRESS, Config.DIRECTION_PORT, 0);
			
		sma.initSma();
			
		//new Thread(direction).start();
		
		
		//loadSmaPanel
		appliWindow.loadSmaPanels();
		
		//Start simulation
		sma.startSimu(appliWindow);
		
		//Launch telemeter
		int inc  = 0;
		for(Telemeter telem : Launcher.listTelemeters) {
			telem.initSMA(sma.getListAgentsObstacle(inc));
			
			new Thread(telem).start();
			inc++;
		}
		
		
	}
	
	/**
	 * Creates several telemeters and add them to the static list
	 * To add or remove telemeters, here are the piece of code to change
	 * 
	 * @since 0.3
	 */
	private void createTelemeters() {
		//Create first telemeter
		Telemeter telemetreThread = new Telemeter(Config.TELEMETER_ADDRESS, Config.TELEMETER_PORT.get(0), 0);
		
		//Create second telemeter
		Telemeter telemetre2Thread = new Telemeter(Config.TELEMETER_ADDRESS, Config.TELEMETER_PORT.get(1), 0);
		//Le second télémètre est orienté vers le bas
		telemetre2Thread.setRotation(180);
		
		//Add them to the list :
		Launcher.listTelemeters.add(telemetreThread);
		Launcher.listTelemeters.add(telemetre2Thread);
	}

}